import 'package:flutter/material.dart';
import 'package:flutter_map_demo/configs/poi_search_base_config.dart';
import 'package:t3_data_service/biz_data_source/map/address/poi_search/poi_search.dart';
import 'package:t3_foundation/t3_foundation.dart';
import 'package:t3_track/t3_track.dart';
import 'package:uikit/uikit.dart';

import '../view_model/poi_search_cell_view_model.dart';
import 'section_model.dart';


class SectionBuilder {
  static var allCorner = const BorderRadius.all(Radius.circular(24));
  static var bottomCorner = const BorderRadius.only(bottomLeft: Radius.circular(24), bottomRight: Radius.circular(24));
  static var topCorner = const BorderRadius.only(topLeft: Radius.circular(24), topRight: Radius.circular(24));

  static List<POIListSection> fromList(T3SearchResult searchResult, POISearchBaseConfig config) {
    List<POIListSection> result = [];
    List<POIModel> list = searchResult.list;
    /// 企业用车
    POIListSection enterpriseSection = POIListSection(T3POICategory.enterprise);
    /// 猜你想去
    POIListSection guessSection = POIListSection(T3POICategory.guess);
    /// 历史记录
    POIListSection historySection = POIListSection(T3POICategory.history);
    /// 推荐地址
    POIListSection recommendSection = POIListSection(T3POICategory.recommend);
    /// 关键字搜索结果
    POIListSection keywordSearchSection = POIListSection(T3POICategory.searchResult);

    for (var element in list) {
      var vm = POISearchCellViewModel(element, config);
      if (searchResult.condition.poiType != T3POIType.start) {
        vm.markNeedNavPoint();
      }
      switch (element.category) {
        case T3POICategory.enterprise:
          enterpriseSection.list.add(vm);
          setShowShareButton(config, enterpriseSection, searchResult);
          break;
        case T3POICategory.guess:
          guessSection.list.add(vm);
          setShowShareButton(config, guessSection, searchResult);
          break;
        case T3POICategory.history:
          historySection.list.add(vm);
          setShowShareButton(config, historySection, searchResult);
          break;
        case T3POICategory.recommend:
          recommendSection.list.add(vm);
          setShowShareButton(config, recommendSection, searchResult);
          break;
        case T3POICategory.searchResult:
          keywordSearchSection.list.add(vm);
          setShowShareButton(config, keywordSearchSection, searchResult);
          break;
      }
    }

    /// 企业用车
    if (enterpriseSection.list.isNotEmpty) {
      enterpriseSection.headerModel = POIListSectionHeaderModel(
          title: searchResult.enterpriseLimitTitle ?? "企业已限制用车地点，出发地或目的地需符合规则",
          imageName: "poi_section_enterprise"
      );

      /// 最多展示3个
      if (enterpriseSection.list.length > 3) {
        enterpriseSection.footerModel = POIListSectionFooterModel(title: "查看全部规范地点", type: POIFooterType.enterprise);
        enterpriseSection.list.first.corner = null;
      } else {
        if (enterpriseSection.list.length > 1) {
          enterpriseSection.list.first.corner = null;
        }
        enterpriseSection.list.last.corner = bottomCorner;
        enterpriseSection.list.last.showDivider = false;
      }
      result.add(enterpriseSection);
    }

    /// 猜你想去
    if (guessSection.list.isNotEmpty) {
      if (guessSection.list.length == 1) {
        guessSection.list.first.corner = allCorner;
      } else {
        guessSection.list.first.corner = topCorner;
        guessSection.list.last.corner = bottomCorner;
      }
      guessSection.list.last.showDivider = false;
      result.add(guessSection);
    }

    /// 历史记录
    if (historySection.list.isNotEmpty) {
      historySection.headerModel = POIListSectionHeaderModel(title: "历史地点", imageName: "poi_section_history");
      historySection.list.last.corner = bottomCorner;
      historySection.list.last.showDivider = false;
      result.add(historySection);
    }

    /// 推荐地址
    if (recommendSection.list.isNotEmpty) {
      recommendSection.headerModel = POIListSectionHeaderModel(title: "推荐地点", imageName: "poi_section_recommend");
      recommendSection.list.last.corner = bottomCorner;
      recommendSection.list.last.showDivider = false;
      result.add(recommendSection);
    }

    /// 关键字搜索结果
    if (keywordSearchSection.list.isNotEmpty) {
      var hasSearchTopText = searchResult.topText != null && searchResult.topText!.isNotEmpty;
      String sectionTitle = '';
      if (config.pageScene == PageScene.feedback) {
        sectionTitle = "请选择您需要报错的地点";
      }
      /// 搜索结果有顶部
      if (hasSearchTopText || sectionTitle.isNotEmpty) {
        if (sectionTitle.isNotEmpty) {
          POIListSectionHeaderModel header = POIListSectionHeaderModel(title: sectionTitle, imageName: null);
          header.backgroundColor = Colors.white;
          header.titleColor = Colors.black;
          keywordSearchSection.headerModel = header;
        } else {
          T3Track.trackExpose('0199.008.000.025');
          POIListSectionHeaderModel header = POIListSectionHeaderModel(title: searchResult.topText!, imageName: null);
          header.backgroundColor = const Color(0xFFFFE7CC).withOpacity(0.6);
          header.titleColor = const Color(0xFF873400);

          if (searchResult.condition.poiType != T3POIType.start) {
            if (config.showConfusionButton) {
              header.actionTitle = "结合地图搜索";
              header.type = POIHeaderType.mapSelect;
            }
          }
          T3Track.trackExpose("0199.008.000.022");
          keywordSearchSection.headerModel = header;
        }
      } else {
        if (keywordSearchSection.list.length == 1) {
          keywordSearchSection.list.first.corner = allCorner;
        } else {
          keywordSearchSection.list.first.corner = topCorner;
        }
      }

      if (config.showMapSelectedFooter) {
        keywordSearchSection.footerModel = POIListSectionFooterModel(title: '去地图上选终点', type: POIFooterType.mapSelect, imageName: T3AssetsName.search_map_entry);
      } else {
        keywordSearchSection.list.last.corner = bottomCorner;
      }

      keywordSearchSection.list.last.showDivider = false;
      result.add(keywordSearchSection);
    }
    var index = 1;
    for (var section in result) {
      for (var poi in section.list) {
        poi.poi.runtimeParams.tableIndex = index;
        index += 1;
      }
    }
    return result;
  }

  static setShowShareButton(POISearchBaseConfig config, POIListSection section,T3SearchResult searchResult) {
    if (config.pageScene != PageScene.feedback) {
      if (searchResult.condition.poiType != T3POIType.start) {
        for (var element in section.list) {
          element.showShareButton = true;
        }
      }
    }
  }
}
